package com.test2.game;

import java.util.ArrayList;
import java.util.Random;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.test2.game.object.Shit;
import com.test2.game.object.Chicken;
import com.test2.game.object.Basket;
import com.test2.game.object.Geese;
import com.test2.game.object.Egg;
import com.test2.game.object.Duck;

public class Test2Game extends ApplicationAdapter {

	SpriteBatch batch;

	// Textures variables
	Texture imgBackGround, line, imgBasket, imgChicken, imgDuck, imgGeese,
			imgEgg, imgShit, lifes, gameover, pause;

	// objects variables
	Basket basket;
	Chicken newChicken;
	Duck newDuck;
	Geese newGeese;

	// State variables of birds
	int stateChicken = 0;
	int stateDuck = 0;
	int stateGeese = 0;

	// Egg state variables of birds
	int stateChickenEgg = 0;
	int stateDuckEgg = 0;
	int stateGeeseEgg = 0;

	// Game state variables
	int stateGameOver = 0;
	int stateGamePause = 0;

	ArrayList<Chicken> listChicken = new ArrayList<Chicken>();
	ArrayList<Duck> listDuck = new ArrayList<Duck>();
	ArrayList<Geese> listGeese = new ArrayList<Geese>();
	ArrayList<Egg> listTrung = new ArrayList<Egg>();

	// Random variables
	float randomNum;
	int randomEggAndShit;

	// Default value of life and level
	int life = 3;

	// Score variables
	int score, scoretemp;
	private String ScoreName;
	BitmapFont BitmapFontScore;

	// Level variables
	int level = 1;
	private String LevelName;
	BitmapFont BitmapFontLevel;

	// Guide variables
	private String GuideName;
	BitmapFont BitmapFontGuide;

	// Speed variable
	float objSpeed;

	@Override
	public void create() {

		// Create Sprite and Textures
		batch = new SpriteBatch();
		imgBackGround = new Texture("background.png");
		line = new Texture("Line.png");
		imgBasket = new Texture("Gio.png");
		imgChicken = new Texture("Ga.png");
		imgDuck = new Texture("Vit.png");
		imgGeese = new Texture("Ngan.png");
		imgShit = new Texture("ChatThai.png");
		imgEgg = new Texture("Trung.png");
		lifes = new Texture("Life.png");
		gameover = new Texture("GameOver.png");
		pause = new Texture("Pause.png");

		// Create basket
		basket = new Basket(230, 40);

		// Set default value for score and tempscore
		score = 0;
		scoretemp = 0;

		// Create bitmaps
		BitmapFontScore = new BitmapFont();
		BitmapFontLevel = new BitmapFont();
		BitmapFontGuide = new BitmapFont();

		// Set start speed for egg and shit
		objSpeed = 0.5f;
	}

	// Handle Animations
	public void objectProcess() {
		Gdx.gl.glClearColor(1, 0, 0, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		batch.begin();

		// Draw background
		batch.draw(imgBackGround, 0, 0);

		// Draw bottom line
		batch.draw(line, 0, -75);

		// Draw top line
		batch.draw(line, 0, 400);

		// Draw basket
		batch.draw(imgBasket, basket.getX(), basket.getY());

		// Draw life
		if (life == 3) {
			batch.draw(lifes, 190, 450);
			batch.draw(lifes, 240, 450);
			batch.draw(lifes, 290, 450);
		} else {
			if (life == 2) {
				batch.draw(lifes, 190, 450);
				batch.draw(lifes, 240, 450);
			} else {
				batch.draw(lifes, 190, 450);
			}
		}

		// Handle actions of Chicken
		if (stateChicken == 1) {
			for (Chicken chickens : listChicken) {

				// Draw Chicken on screen
				batch.draw(imgChicken, chickens.getX(), chickens.getY());

				if (chickens.moveState() == 1) {
					chickens.setPosition(chickens.getX() - 4, chickens.getY());
				}
				if (chickens.moveState() == 0) {
					chickens.setPosition(chickens.getX() + 4, chickens.getY());
				}

				// After 5s, create egg or shit of Chicken
				if ((System.currentTimeMillis() - chickens.getBornTime()) >= 5000) {
					chickens.setBornTime(System.currentTimeMillis());
					stateChickenEgg = 1;

					Random randEgg = new Random();
					randomEggAndShit = randEgg.nextInt(((100 - 1) + 1) + 1);

					if (randomEggAndShit > 30) {
						Egg newChickenEgg = new Egg(chickens.getX(),
								chickens.getY());
						newChickenEgg.setState(1);
						chickens.getListTrungGa().add(newChickenEgg);
					} else {
						Shit newChickenShit = new Shit(chickens.getX(),
								chickens.getY());
						newChickenShit.setState(1);
						chickens.getListChatThaiGa().add(newChickenShit);
					}
				}

				// Handle actions of egg and shit
				if (stateChickenEgg == 1) {

					// Handle egg of each duck
					for (Egg chickenEgg : chickens.getListTrungGa()) {
						if (chickenEgg.getState() == 1) {

							// Draw egg on screen
							batch.draw(imgEgg, chickenEgg.getX(),
									chickenEgg.getY());

							if (chickenEgg.getY() <= 65
									&& (chickenEgg.getX() + 17.5) > basket
											.getX()
									&& (chickenEgg.getX() + 17.5) < (basket
											.getX() + 67)) {
								chickenEgg.setState(0);
								score++;
								scoretemp++;
							}
							if (chickenEgg.getY() > 35) {
								chickenEgg.setPosition(chickenEgg.getX(),
										chickenEgg.getY() - objSpeed);
							} else {
								chickenEgg.setState(0);
								life--;
							}
						}
					}

					// Handle shit of each chicken
					for (Shit chickenShit : chickens.getListChatThaiGa()) {
						if (chickenShit.getState() == 1) {

							// Draw shit on screen
							batch.draw(imgShit, chickenShit.getX(),
									chickenShit.getY());
							if (chickenShit.getY() <= 65
									&& (chickenShit.getX() + 17.5) > basket
											.getX()
									&& (chickenShit.getX() + 17.5) < (basket
											.getX() + 67)) {
								chickenShit.setState(0);
								life--;
							}
							if (chickenShit.getY() > 35) {
								chickenShit.setPosition(chickenShit.getX(),
										chickenShit.getY() - objSpeed);
							} else {
								chickenShit.setState(0);
							}
						}
					}
				}
			}
		}

		// Handle actions of Duck
		if (stateDuck == 1) {
			for (Duck ducks : listDuck) {

				// Draw Duck on screen
				batch.draw(imgDuck, ducks.getX(), ducks.getY());

				if (ducks.moveState() == 1) {
					ducks.setPosition(ducks.getX() - 3, ducks.getY());
				}
				if (ducks.moveState() == 0) {
					ducks.setPosition(ducks.getX() + 3, ducks.getY());
				}

				// After 5s, create egg or shit of Duck
				if ((System.currentTimeMillis() - ducks.getBornTime()) >= 5000) {
					ducks.setBornTime(System.currentTimeMillis());
					stateDuckEgg = 1;

					Random randEgg = new Random();
					randomEggAndShit = randEgg.nextInt(((100 - 1) + 1) + 1);

					if (randomEggAndShit > 30) {
						Egg newDuckEgg = new Egg(ducks.getX(), ducks.getY());
						newDuckEgg.setState(1);
						ducks.getListTrungVit().add(newDuckEgg);
					} else {
						Shit newDuckShit = new Shit(ducks.getX(), ducks.getY());
						newDuckShit.setState(1);
						ducks.getListChatThaiVit().add(newDuckShit);
					}
				}

				// Handle actions of egg and shit
				if (stateDuckEgg == 1) {

					// Handle egg of each duck
					for (Egg duckEgg : ducks.getListTrungVit()) {
						if (duckEgg.getState() == 1) {

							// Draw egg on screen
							batch.draw(imgEgg, duckEgg.getX(), duckEgg.getY());

							if (duckEgg.getY() <= 65
									&& (duckEgg.getX() + 17.5) > basket.getX()
									&& (duckEgg.getX() + 17.5) < (basket.getX() + 67)) {
								duckEgg.setState(0);
								score++;
								scoretemp++;
							}
							if (duckEgg.getY() > 35) {
								duckEgg.setPosition(duckEgg.getX(),
										duckEgg.getY() - objSpeed);
							} else {
								duckEgg.setState(0);
								life--;
							}
						}
					}

					// Handle shit of each duck
					for (Shit duckShit : ducks.getListChatThaiVit()) {
						if (duckShit.getState() == 1) {

							// Draw shit on screen
							batch.draw(imgShit, duckShit.getX(),
									duckShit.getY());

							if (duckShit.getY() <= 65
									&& (duckShit.getX() + 17.5) > basket.getX()
									&& (duckShit.getX() + 17.5) < (basket
											.getX() + 67)) {
								duckShit.setState(0);
								life--;
							}
							if (duckShit.getY() > 35) {
								duckShit.setPosition(duckShit.getX(),
										duckShit.getY() - objSpeed);
							} else {
								duckShit.setState(0);
							}
						}
					}
				}
			}
		}

		// Handle actions of Geese
		if (stateGeese == 1) {
			for (Geese geeses : listGeese) {

				// Draw Geese on screen
				batch.draw(imgGeese, geeses.getX(), geeses.getY());

				if (geeses.moveState() == 1) {
					geeses.setPosition(geeses.getX() - 2, geeses.getY());
				}
				if (geeses.moveState() == 0) {
					geeses.setPosition(geeses.getX() + 2, geeses.getY());
				}

				// After 5s, create egg or shit of Geese
				if ((System.currentTimeMillis() - geeses.getBornTime()) >= 5000) {
					geeses.setBornTime(System.currentTimeMillis());
					stateGeeseEgg = 1;

					Random randEgg = new Random();
					randomEggAndShit = randEgg.nextInt(((100 - 1) + 1) + 1);

					if (randomEggAndShit > 30) {
						Egg newGeeseEgg = new Egg(geeses.getX(), geeses.getY());
						newGeeseEgg.setState(1);
						geeses.getListTrungNgan().add(newGeeseEgg);
					} else {
						Shit newGeeseShit = new Shit(geeses.getX(),
								geeses.getY());
						newGeeseShit.setState(1);
						geeses.getListChatThaiNgan().add(newGeeseShit);
					}
				}

				// Handle actions of egg and shit
				if (stateGeeseEgg == 1) {
					// Handle egg of each Geese
					for (Egg geeseEgg : geeses.getListTrungNgan()) {
						if (geeseEgg.getState() == 1) {

							// Draw egg on screen
							batch.draw(imgEgg, geeseEgg.getX(), geeseEgg.getY());

							if (geeseEgg.getY() <= 65
									&& (geeseEgg.getX() + 17.5) > basket.getX()
									&& (geeseEgg.getX() + 17.5) < (basket
											.getX() + 67)) {
								geeseEgg.setState(0);
								score++;
								scoretemp++;
							}
							if (geeseEgg.getY() > 35) {
								geeseEgg.setPosition(geeseEgg.getX(),
										geeseEgg.getY() - objSpeed);
							} else {
								geeseEgg.setState(0);
								life--;
							}
						}
					}
					// Handle shit of each Geese
					for (Shit geeseShit : geeses.getListChatThaiNgan()) {
						if (geeseShit.getState() == 1) {

							// Draw shit on screen
							batch.draw(imgShit, geeseShit.getX(),
									geeseShit.getY());

							if (geeseShit.getY() <= 65
									&& (geeseShit.getX() + 17.5) > basket
											.getX()
									&& (geeseShit.getX() + 17.5) < (basket
											.getX() + 67)) {
								geeseShit.setState(0);
								life--;
							}
							if (geeseShit.getY() > 35) {
								geeseShit.setPosition(geeseShit.getX(),
										geeseShit.getY() - objSpeed);
							} else {
								geeseShit.setState(0);
							}
						}
					}
				}
			}
		}

		// Notify game over
		if (life == 0) {
			stateGameOver = 1;
			if (stateGameOver == 1) {
				// Draw game over background
				batch.draw(gameover, 0, 0);
			}

			// Set state variables to default
			stateChicken = 0;
			stateDuck = 0;
			stateGeese = 0;
			stateChickenEgg = 0;
			stateDuckEgg = 0;
			stateGeeseEgg = 0;

			// Deleting objects in ArrayList
			listChicken.clear();
			listDuck.clear();
			listGeese.clear();
		}

		// Increase level and speed of egg, shit per 20 point
		if (scoretemp == 20) {
			level++;
			objSpeed += 0.2f;
			scoretemp = 0;
		}

		// Check the pause status
		if (stateGamePause == 1) {
			batch.draw(pause, 150, 200);
		}

		// Show the level on the screen
		LevelName = "Level: " + level;
		BitmapFontLevel.setColor(1.0f, 0f, 1.0f, 1.0f);
		BitmapFontLevel.draw(batch, LevelName, 120, 470);

		// Show the guide on the screen
		GuideName = "G:Add Chicken   V:Add Duck   N:Add Geese   P:Pause   ESC:Exit";
		BitmapFontGuide.setColor(1.0f, 0f, 0f, 1.0f);
		BitmapFontGuide.draw(batch, GuideName, 45, 15);

		// Show the score on the screen
		ScoreName = "score: " + score;
		BitmapFontScore.setColor(1.0f, 0f, 1.0f, 1.0f);
		BitmapFontScore.draw(batch, ScoreName, 340, 470);
		batch.end();
	}

	@Override
	public void render() {
		try {
			addObject();
			objectProcess();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// Handle keyboard input
	private void addObject() {

		// Handle the moving of basket when press Right
		if (Gdx.input.isKeyPressed(Keys.RIGHT)) {
			if (basket.getX() < 430) {
				basket.setPosition(basket.getX() + 5, basket.getY());
			}
		}

		// Handle the moving of basket when press Left
		if (Gdx.input.isKeyPressed(Keys.LEFT)) {
			if (basket.getX() > 3) {
				basket.setPosition(basket.getX() - 5, basket.getY());
			}
		}

		// Add a chicken when press G
		if (Gdx.input.isKeyJustPressed(Keys.G)) {
			stateChicken = 1;
			Random rand1 = new Random();
			randomNum = rand1.nextInt((445 - 0) + 1) + 0;
			newChicken = new Chicken(randomNum, 510);
			listChicken.add(newChicken);
		}

		// Add a Duck when press V
		if (Gdx.input.isKeyJustPressed(Keys.V)) {
			stateDuck = 1;
			Random rand2 = new Random();
			randomNum = rand2.nextInt((445 - 0) + 1) + 0;
			newDuck = new Duck(randomNum, 510);
			listDuck.add(newDuck);
		}

		// Add a Geese when press N
		if (Gdx.input.isKeyJustPressed(Keys.N)) {
			stateGeese = 1;
			Random rand3 = new Random();
			randomNum = rand3.nextInt((445 - 0) + 1) + 0;
			newGeese = new Geese(randomNum, 510);
			listGeese.add(newGeese);
		}

		// Quit game when press ESC
		if (Gdx.input.isKeyJustPressed(Keys.ESCAPE)) {
			Gdx.app.exit();
		}

		// Restart game when press R
		if (Gdx.input.isKeyJustPressed(Keys.R)) {
			score = 0;
			scoretemp = 0;
			level = 1;
			life = 3;
			stateGameOver = 0;
		}

		// Pause game when press P
		if (Gdx.input.isKeyJustPressed(Keys.P)) {
			if (stateGamePause == 0) {
				Gdx.graphics.setContinuousRendering(false);
				stateGamePause = 1;
			} else {
				Gdx.graphics.setContinuousRendering(true);
				stateGamePause = 0;
			}
		}
	}
}
